package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.FeedDAO;
import cn.edu.scau.cmi.hombio.dao.InspectionExceptionDAO;
import cn.edu.scau.cmi.hombio.dao.PigHouseDAO;
import cn.edu.scau.cmi.hombio.dao.PigstyDAO;
import cn.edu.scau.cmi.hombio.dao.PreventionDAO;
import cn.edu.scau.cmi.hombio.dao.TaskPigstyDAO;
import cn.edu.scau.cmi.hombio.dao.TransferDAO;

import cn.edu.scau.cmi.hombio.domain.Feed;
import cn.edu.scau.cmi.hombio.domain.InspectionException;
import cn.edu.scau.cmi.hombio.domain.PigHouse;
import cn.edu.scau.cmi.hombio.domain.Pigsty;
import cn.edu.scau.cmi.hombio.domain.Prevention;
import cn.edu.scau.cmi.hombio.domain.TaskPigsty;
import cn.edu.scau.cmi.hombio.domain.Transfer;

import cn.edu.scau.cmi.hombio.service.PigstyService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for Pigsty entities
 * 
 */

@Controller("PigstyRestController")

public class PigstyRestController {

	/**
	 * DAO injected by Spring that manages Feed entities
	 * 
	 */
	@Autowired
	private FeedDAO feedDAO;

	/**
	 * DAO injected by Spring that manages InspectionException entities
	 * 
	 */
	@Autowired
	private InspectionExceptionDAO inspectionExceptionDAO;

	/**
	 * DAO injected by Spring that manages PigHouse entities
	 * 
	 */
	@Autowired
	private PigHouseDAO pigHouseDAO;

	/**
	 * DAO injected by Spring that manages Pigsty entities
	 * 
	 */
	@Autowired
	private PigstyDAO pigstyDAO;

	/**
	 * DAO injected by Spring that manages Prevention entities
	 * 
	 */
	@Autowired
	private PreventionDAO preventionDAO;

	/**
	 * DAO injected by Spring that manages TaskPigsty entities
	 * 
	 */
	@Autowired
	private TaskPigstyDAO taskPigstyDAO;

	/**
	 * DAO injected by Spring that manages Transfer entities
	 * 
	 */
	@Autowired
	private TransferDAO transferDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Pigsty entities
	 * 
	 */
	@Autowired
	private PigstyService pigstyService;

	/**
	 * Show all Transfer entities by Pigsty
	 * 
	 */
	@RequestMapping(value = "/Pigsty/{pigsty_id}/transfers", method = RequestMethod.GET)
	@ResponseBody
	public List<Transfer> getPigstyTransfers(@PathVariable Integer pigsty_id) {
		return new java.util.ArrayList<Transfer>(pigstyDAO.findPigstyByPrimaryKey(pigsty_id).getTransfers());
	}

	/**
	* Show all TaskPigsty entities by Pigsty
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/taskPigsties", method = RequestMethod.GET)
	@ResponseBody
	public List<TaskPigsty> getPigstyTaskPigsties(@PathVariable Integer pigsty_id) {
		return new java.util.ArrayList<TaskPigsty>(pigstyDAO.findPigstyByPrimaryKey(pigsty_id).getTaskPigsties());
	}

	/**
	* Save an existing TaskPigsty entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/taskPigsties", method = RequestMethod.PUT)
	@ResponseBody
	public TaskPigsty savePigstyTaskPigsties(@PathVariable Integer pigsty_id, @RequestBody TaskPigsty taskpigsties) {
		pigstyService.savePigstyTaskPigsties(pigsty_id, taskpigsties);
		return taskPigstyDAO.findTaskPigstyByPrimaryKey(taskpigsties.getId());
	}

	/**
	* Save an existing InspectionException entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/inspectionExceptions", method = RequestMethod.PUT)
	@ResponseBody
	public InspectionException savePigstyInspectionExceptions(@PathVariable Integer pigsty_id, @RequestBody InspectionException inspectionexceptions) {
		pigstyService.savePigstyInspectionExceptions(pigsty_id, inspectionexceptions);
		return inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(inspectionexceptions.getId());
	}

	/**
	* Create a new InspectionException entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/inspectionExceptions", method = RequestMethod.POST)
	@ResponseBody
	public InspectionException newPigstyInspectionExceptions(@PathVariable Integer pigsty_id, @RequestBody InspectionException inspectionexception) {
		pigstyService.savePigstyInspectionExceptions(pigsty_id, inspectionexception);
		return inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(inspectionexception.getId());
	}

	/**
	* Delete an existing Prevention entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/preventions/{prevention_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePigstyPreventions(@PathVariable Integer pigsty_id, @PathVariable Integer related_preventions_id) {
		pigstyService.deletePigstyPreventions(pigsty_id, related_preventions_id);
	}

	/**
	* View an existing Feed entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/feeds/{feed_id}", method = RequestMethod.GET)
	@ResponseBody
	public Feed loadPigstyFeeds(@PathVariable Integer pigsty_id, @PathVariable Integer related_feeds_id) {
		Feed feed = feedDAO.findFeedByPrimaryKey(related_feeds_id, -1, -1);

		return feed;
	}

	/**
	* Show all InspectionException entities by Pigsty
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/inspectionExceptions", method = RequestMethod.GET)
	@ResponseBody
	public List<InspectionException> getPigstyInspectionExceptions(@PathVariable Integer pigsty_id) {
		return new java.util.ArrayList<InspectionException>(pigstyDAO.findPigstyByPrimaryKey(pigsty_id).getInspectionExceptions());
	}

	/**
	* Select an existing Pigsty entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}", method = RequestMethod.GET)
	@ResponseBody
	public Pigsty loadPigsty(@PathVariable Integer pigsty_id) {
		return pigstyDAO.findPigstyByPrimaryKey(pigsty_id);
	}

	/**
	* Create a new Pigsty entity
	* 
	*/
	@RequestMapping(value = "/Pigsty", method = RequestMethod.POST)
	@ResponseBody
	public Pigsty newPigsty(@RequestBody Pigsty pigsty) {
		pigstyService.savePigsty(pigsty);
		return pigstyDAO.findPigstyByPrimaryKey(pigsty.getId());
	}

	/**
	* View an existing TaskPigsty entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/taskPigsties/{taskpigsty_id}", method = RequestMethod.GET)
	@ResponseBody
	public TaskPigsty loadPigstyTaskPigsties(@PathVariable Integer pigsty_id, @PathVariable Integer related_taskpigsties_id) {
		TaskPigsty taskpigsty = taskPigstyDAO.findTaskPigstyByPrimaryKey(related_taskpigsties_id, -1, -1);

		return taskpigsty;
	}

	/**
	* Create a new PigHouse entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/pigHouse", method = RequestMethod.POST)
	@ResponseBody
	public PigHouse newPigstyPigHouse(@PathVariable Integer pigsty_id, @RequestBody PigHouse pighouse) {
		pigstyService.savePigstyPigHouse(pigsty_id, pighouse);
		return pigHouseDAO.findPigHouseByPrimaryKey(pighouse.getId());
	}

	/**
	* Create a new Prevention entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/preventions", method = RequestMethod.POST)
	@ResponseBody
	public Prevention newPigstyPreventions(@PathVariable Integer pigsty_id, @RequestBody Prevention prevention) {
		pigstyService.savePigstyPreventions(pigsty_id, prevention);
		return preventionDAO.findPreventionByPrimaryKey(prevention.getId());
	}

	/**
	* Delete an existing Transfer entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/transfers/{transfer_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePigstyTransfers(@PathVariable Integer pigsty_id, @PathVariable Integer related_transfers_id) {
		pigstyService.deletePigstyTransfers(pigsty_id, related_transfers_id);
	}

	/**
	* Show all Prevention entities by Pigsty
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/preventions", method = RequestMethod.GET)
	@ResponseBody
	public List<Prevention> getPigstyPreventions(@PathVariable Integer pigsty_id) {
		return new java.util.ArrayList<Prevention>(pigstyDAO.findPigstyByPrimaryKey(pigsty_id).getPreventions());
	}

	/**
	* Delete an existing Feed entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/feeds/{feed_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePigstyFeeds(@PathVariable Integer pigsty_id, @PathVariable Integer related_feeds_id) {
		pigstyService.deletePigstyFeeds(pigsty_id, related_feeds_id);
	}

	/**
	* Save an existing PigHouse entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/pigHouse", method = RequestMethod.PUT)
	@ResponseBody
	public PigHouse savePigstyPigHouse(@PathVariable Integer pigsty_id, @RequestBody PigHouse pighouse) {
		pigstyService.savePigstyPigHouse(pigsty_id, pighouse);
		return pigHouseDAO.findPigHouseByPrimaryKey(pighouse.getId());
	}

	/**
	* Create a new Feed entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/feeds", method = RequestMethod.POST)
	@ResponseBody
	public Feed newPigstyFeeds(@PathVariable Integer pigsty_id, @RequestBody Feed feed) {
		pigstyService.savePigstyFeeds(pigsty_id, feed);
		return feedDAO.findFeedByPrimaryKey(feed.getId());
	}

	/**
	* View an existing PigHouse entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/pigHouse/{pighouse_id}", method = RequestMethod.GET)
	@ResponseBody
	public PigHouse loadPigstyPigHouse(@PathVariable Integer pigsty_id, @PathVariable Integer related_pighouse_id) {
		PigHouse pighouse = pigHouseDAO.findPigHouseByPrimaryKey(related_pighouse_id, -1, -1);

		return pighouse;
	}

	/**
	* Delete an existing InspectionException entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/inspectionExceptions/{inspectionexception_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePigstyInspectionExceptions(@PathVariable Integer pigsty_id, @PathVariable Integer related_inspectionexceptions_id) {
		pigstyService.deletePigstyInspectionExceptions(pigsty_id, related_inspectionexceptions_id);
	}

	/**
	* Save an existing Feed entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/feeds", method = RequestMethod.PUT)
	@ResponseBody
	public Feed savePigstyFeeds(@PathVariable Integer pigsty_id, @RequestBody Feed feeds) {
		pigstyService.savePigstyFeeds(pigsty_id, feeds);
		return feedDAO.findFeedByPrimaryKey(feeds.getId());
	}

	/**
	* Show all Pigsty entities
	* 
	*/
	@RequestMapping(value = "/Pigsty", method = RequestMethod.GET)
	@ResponseBody
	public List<Pigsty> listPigstys() {
		return new java.util.ArrayList<Pigsty>(pigstyService.loadPigstys());
	}

	/**
	* Save an existing Prevention entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/preventions", method = RequestMethod.PUT)
	@ResponseBody
	public Prevention savePigstyPreventions(@PathVariable Integer pigsty_id, @RequestBody Prevention preventions) {
		pigstyService.savePigstyPreventions(pigsty_id, preventions);
		return preventionDAO.findPreventionByPrimaryKey(preventions.getId());
	}

	/**
	* Delete an existing PigHouse entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/pigHouse/{pighouse_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePigstyPigHouse(@PathVariable Integer pigsty_id, @PathVariable Integer related_pighouse_id) {
		pigstyService.deletePigstyPigHouse(pigsty_id, related_pighouse_id);
	}

	/**
	* Get PigHouse entity by Pigsty
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/pigHouse", method = RequestMethod.GET)
	@ResponseBody
	public PigHouse getPigstyPigHouse(@PathVariable Integer pigsty_id) {
		return pigstyDAO.findPigstyByPrimaryKey(pigsty_id).getPigHouse();
	}

	/**
	* Delete an existing Pigsty entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePigsty(@PathVariable Integer pigsty_id) {
		Pigsty pigsty = pigstyDAO.findPigstyByPrimaryKey(pigsty_id);
		pigstyService.deletePigsty(pigsty);
	}

	/**
	* Save an existing Pigsty entity
	* 
	*/
	@RequestMapping(value = "/Pigsty", method = RequestMethod.PUT)
	@ResponseBody
	public Pigsty savePigsty(@RequestBody Pigsty pigsty) {
		pigstyService.savePigsty(pigsty);
		return pigstyDAO.findPigstyByPrimaryKey(pigsty.getId());
	}

	/**
	* Show all Feed entities by Pigsty
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/feeds", method = RequestMethod.GET)
	@ResponseBody
	public List<Feed> getPigstyFeeds(@PathVariable Integer pigsty_id) {
		return new java.util.ArrayList<Feed>(pigstyDAO.findPigstyByPrimaryKey(pigsty_id).getFeeds());
	}

	/**
	* View an existing Transfer entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/transfers/{transfer_id}", method = RequestMethod.GET)
	@ResponseBody
	public Transfer loadPigstyTransfers(@PathVariable Integer pigsty_id, @PathVariable Integer related_transfers_id) {
		Transfer transfer = transferDAO.findTransferByPrimaryKey(related_transfers_id, -1, -1);

		return transfer;
	}

	/**
	* Save an existing Transfer entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/transfers", method = RequestMethod.PUT)
	@ResponseBody
	public Transfer savePigstyTransfers(@PathVariable Integer pigsty_id, @RequestBody Transfer transfers) {
		pigstyService.savePigstyTransfers(pigsty_id, transfers);
		return transferDAO.findTransferByPrimaryKey(transfers.getId());
	}

	/**
	* Create a new TaskPigsty entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/taskPigsties", method = RequestMethod.POST)
	@ResponseBody
	public TaskPigsty newPigstyTaskPigsties(@PathVariable Integer pigsty_id, @RequestBody TaskPigsty taskpigsty) {
		pigstyService.savePigstyTaskPigsties(pigsty_id, taskpigsty);
		return taskPigstyDAO.findTaskPigstyByPrimaryKey(taskpigsty.getId());
	}

	/**
	* Delete an existing TaskPigsty entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/taskPigsties/{taskpigsty_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePigstyTaskPigsties(@PathVariable Integer pigsty_id, @PathVariable Integer related_taskpigsties_id) {
		pigstyService.deletePigstyTaskPigsties(pigsty_id, related_taskpigsties_id);
	}

	/**
	* View an existing Prevention entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/preventions/{prevention_id}", method = RequestMethod.GET)
	@ResponseBody
	public Prevention loadPigstyPreventions(@PathVariable Integer pigsty_id, @PathVariable Integer related_preventions_id) {
		Prevention prevention = preventionDAO.findPreventionByPrimaryKey(related_preventions_id, -1, -1);

		return prevention;
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* View an existing InspectionException entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/inspectionExceptions/{inspectionexception_id}", method = RequestMethod.GET)
	@ResponseBody
	public InspectionException loadPigstyInspectionExceptions(@PathVariable Integer pigsty_id, @PathVariable Integer related_inspectionexceptions_id) {
		InspectionException inspectionexception = inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(related_inspectionexceptions_id, -1, -1);

		return inspectionexception;
	}

	/**
	* Create a new Transfer entity
	* 
	*/
	@RequestMapping(value = "/Pigsty/{pigsty_id}/transfers", method = RequestMethod.POST)
	@ResponseBody
	public Transfer newPigstyTransfers(@PathVariable Integer pigsty_id, @RequestBody Transfer transfer) {
		pigstyService.savePigstyTransfers(pigsty_id, transfer);
		return transferDAO.findTransferByPrimaryKey(transfer.getId());
	}
}